/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Reportes.java
 *
 * Created on 31/03/2011, 08:49:01 AM
 */

package com.catosist.run.application.reportes;

import com.catosist.run.application.Util.ReporteCompraDataSource;
import com.catosist.run.application.Util.ReporteVentaDataSource;
import com.catosist.run.business.gestorcomprobante.GestorComprobante;
import com.catosist.run.business.gestorgasto.GestorGasto;
import com.catosist.run.services.ResultadoCompraDTO;
import com.catosist.run.services.ResultadoVentaDTO;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JPanel;

import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperCompileManager;
import java.util.HashMap;
import java.util.Map;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;

/**
 *
 * @author igorov
 */
public class Reportes extends javax.swing.JFrame {

    /** Creates new form Reportes */
    public Reportes() {
        initComponents();
    }

    public Reportes(GestorComprobante gestorComprobante, GestorGasto gestorGasto) {
        initComponents();
        this.gestorComprobante = gestorComprobante;
        this.gestorGasto = gestorGasto;
    }

    public JPanel getPanel(){
        return jPanel1;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnReporteVentas = new javax.swing.JButton();
        btnReporteCompras = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.catosist.run.application.desktopapplication.DesktopApplication.class).getContext().getResourceMap(Reportes.class);
        btnReporteVentas.setText(resourceMap.getString("btnReporteVentas.text")); // NOI18N
        btnReporteVentas.setName("btnReporteVentas"); // NOI18N
        btnReporteVentas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReporteVentasActionPerformed(evt);
            }
        });

        btnReporteCompras.setText(resourceMap.getString("btnReporteCompras.text")); // NOI18N
        btnReporteCompras.setName("btnReporteCompras"); // NOI18N
        btnReporteCompras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReporteComprasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btnReporteVentas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnReporteCompras, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(138, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(btnReporteVentas)
                .addGap(18, 18, 18)
                .addComponent(btnReporteCompras)
                .addContainerGap(196, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnReporteVentasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReporteVentasActionPerformed
        // TODO add your handling code here:
        ReporteVentaDataSource datasource = new ReporteVentaDataSource();
//        List<ResultadoVentaDTO> resultadoVentaDTOs = new LinkedList<ResultadoVentaDTO>();
        List<Object> objects = gestorComprobante.reporteVentas();

        for (int i = 0; i < objects.size(); i++) {
            Object[] os = (Object[]) objects.get(i);

            ResultadoVentaDTO resultadoVentaDTO = new ResultadoVentaDTO(
                    Integer.parseInt(os[0].toString()),
                    Integer.parseInt(os[1].toString()),
                    Float.parseFloat(os[2].toString()),
                    Float.parseFloat(os[3].toString()),
                    Float.parseFloat(os[4].toString()),
                    Float.parseFloat(os[5].toString()),
                    Float.parseFloat(os[6].toString()));

//            resultadoVentaDTOs.add(resultadoVentaDTO);
            datasource.addResultado(resultadoVentaDTO);
        }

        try {
            JasperReport reporte = (JasperReport) JRLoader.loadObject("ReporteVentas.jasper");
            JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null, datasource);

//            JRExporter exporter = new JRPdfExporter();
//            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
//            exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("ReporteVentas.pdf"));
//            exporter.exportReport();

            // Exporta el informe a PDF
            String home = System.getProperties().getProperty("user.home");
            Calendar actual = Calendar.getInstance();
            JasperExportManager.exportReportToPdfFile(jasperPrint, home + "/Escritorio/ReporteVentas_" + actual.get(Calendar.DATE) + "_" + (actual.get(Calendar.MONTH) + 1) +".pdf");
            //Para visualizar el pdf directamente desde java
            JasperViewer.viewReport(jasperPrint, false);
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }      

    }//GEN-LAST:event_btnReporteVentasActionPerformed

    private void btnReporteComprasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReporteComprasActionPerformed
        // TODO add your handling code here:
        ReporteCompraDataSource datasource = new ReporteCompraDataSource();
        List<Object> objects = gestorGasto.reporteCompras();

        for (int i = 0; i < objects.size(); i++) {
            Object[] os = (Object[]) objects.get(i);

            ResultadoCompraDTO resultadoCompraDTO = new ResultadoCompraDTO(
                    Integer.parseInt(os[0].toString()),
                    Integer.parseInt(os[1].toString()),
                    Float.parseFloat(os[2].toString()),
                    Float.parseFloat(os[3].toString()));

            datasource.addResultado(resultadoCompraDTO);
        }

        try {
            JasperReport reporte = (JasperReport) JRLoader.loadObject("ReporteCompras.jasper");
            JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null, datasource);

            // Exporta el informe a PDF
            String home = System.getProperties().getProperty("user.home");
            Calendar actual = Calendar.getInstance();
            JasperExportManager.exportReportToPdfFile(jasperPrint, home + "/Escritorio/ReporteCompras_" + actual.get(Calendar.DATE) + "_" + (actual.get(Calendar.MONTH) + 1) +".pdf");
            //Para visualizar el pdf directamente desde java
            JasperViewer.viewReport(jasperPrint, false);
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }//GEN-LAST:event_btnReporteComprasActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Reportes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReporteCompras;
    private javax.swing.JButton btnReporteVentas;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    private GestorComprobante gestorComprobante;
    private GestorGasto gestorGasto;
    static Connection conn = null;
}
